#include "co2warner.h"



void read_Temp() {
	// initialize the ADC
	adc_init();

	// read and convert temperature
	temp = adc_convertTemp(adc_read(ADC_TEMP_CH));

	// set the prower reduction on
	adc_disable();

	// display temperature
	lcd_setCursor(0, 0);
	fprintf(lcdout, "temp= %d.%d \n", temp/10, temp%10);

	scheduler_add(read_Temp, ONE_SECOND);
}

void read_CO2() {

	// initialize the ADC
	adc_init();

	// read CO2
	co2 = adc_read(ADC_CO2_CH) *CO2_GAIN_ERR;
	if (co2>THRESHOLD)
		co2=THRESHOLD;

	// set the prower reduction on
	adc_disable();

	// display CO2
	lcd_setCursor(0, 2);
	fprintf(lcdout, "co2= %4d ppm \n", co2);

	led_redOff();
	led_yellowOff();
	led_greenOff();

	if (co2 < GREEN_LEVEL) {
		led_greenOn();
	} else if (co2 >= GREEN_LEVEL && co2 < YELLOW_GREEN_LEVEL) {
		led_greenOn();
		led_yellowOn();
	} else if (co2 >= YELLOW_GREEN_LEVEL && co2 < YELLOW_LEVEL) {
		led_yellowOn();
	} else if (co2 >= YELLOW_LEVEL && co2 < RED_YELLOW_LEVEL) {
		led_yellowOn();
		led_redOn();
	} else if (co2 >= RED_YELLOW_LEVEL) {
		led_redOn();
	}

	scheduler_add(read_CO2, ONE_SECOND);
}



int main() {

	// iniatilize phase
	lcd_init();
	lcd_clear();
	led_allInit();
	scheduler_init();

	scheduler_add(read_CO2, IMMEDIATELY);
	scheduler_add(read_Temp, ONE_SECOND);
	scheduler_run();

	return 0;
}
